﻿
Imports System.IO
Imports xmlctrl
Imports xhtmlctrl
Imports webctrl
Imports storoomc

Public Class viewcomment
    Inherits requestProcessor

    Public Const _pathidentity As String = "/viewcomment"
    Public Const keyquery As String = "key"
    Public Const nocomment As String = "cannot get existing comment now."

    Public Overrides Function pathidentity() As String
        Return _pathidentity
    End Function

    Protected Function readcomment(ByVal key As String) As String
        Dim bs As bytes = Nothing
        Dim rtn As String = Nothing
        If storoom.read(key, bs) Then
            rtn = encoding.utf8.GetString(bs.data)
        Else
            raiseError("cannot read existing comment, key " + key + ", use default string." _
                        , errorHandle.errorType.warning)
            rtn = nocomment
        End If
        rtn = htmlUtils.escape(rtn)

        Return rtn
    End Function

    Public Overrides Function process(ByVal context As httpContext) As Boolean
        Dim buff As MemoryStream = Nothing
        buff = context.buff()

        Dim key As String = Nothing
        key = context.url.query(keyquery)

        Dim page As xhtmlpage = Nothing
        page = New xhtmlpage(buff)
        page.title.text = applicationSign() + " - viewcomment"
        page.addcsslink(csslinkhref)

        Dim pre As pre = Nothing
        pre = New pre(buff)
        pre.text = readcomment(key)
        page.body().addchild(pre)

        context.setstatuscode(Net.HttpStatusCode.OK, "OK")
        context.setcontentEncoding(encoding.utf8)
        context.setcontentType(network.http.contentType.texthtmlUtf8)

        If page.render() Then
            Return True
        Else
            raiseError("failed to render xhtmlpage, ignore request.", errorHandle.errorType.warning)
            Return False
        End If
    End Function
End Class
